package org.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.example.entity.Account;
import org.example.entity.Authority;
import org.example.entity.CustomerAuthority;
import org.example.mapper.AccountMapper;
import org.example.mapper.CustomerAuthorityMapper;
import org.example.service.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AccountServiceImpl implements AccountService {

    @Autowired
    AccountMapper accountMapper;
    @Autowired
    CustomerAuthorityMapper customerAuthorityMapper;

    @Override
    public Account loginByUserName(String username) {

        QueryWrapper wrapper = new QueryWrapper<>();
        wrapper.eq("username", username);
       return (Account) accountMapper.selectOne(wrapper);

    }
    @Override
    public int register(Account account) {
         int i =   accountMapper.insert(account);
        Integer cid = account.getId();
        CustomerAuthority customerAuthority  =new CustomerAuthority();
        customerAuthority.setCustomerId(cid);
        customerAuthority.setId(0);
        customerAuthority.setAuthorityId(1);
      return  customerAuthorityMapper.insert(customerAuthority);

    }

    @Override
    public List<Account> queryAllUser() {
        return  accountMapper.selectList(null);

    }

    @Override
    public Account login(Account account) {
        QueryWrapper<Account> wrapper = new QueryWrapper<>();
        wrapper.setEntity(account);
        return (Account) accountMapper.selectOne(wrapper);

    }
    @Override
    public List<Authority> getAccountAuthority(String username) {
        return accountMapper.findAuthoritiesByUsername(username);
    }

}